import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../components/Login.vue')
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('../components/Home.vue'),
        // 进入home页面直接重定向到我们的welcome页面
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                name: 'Welcome',
                component: () => import('../components/Welcome.vue')
            },
            {
                path: '/userList',
                name: 'User',
                component: () => import('../components/system/User.vue')
            },
            {
                path: '/roleList',
                name: 'Role',
                component: () => import('../components/system/Role.vue')
            },
            {
                path: '/permList',
                name: 'Perm',
                component: () => import('../components/system/Perm.vue')
            },
            {
                path: '/goodsList',
                name: 'GoodsList',
                component: () => import('../components/goods/GoodsList.vue')
            },
            {
                path: '/addGoods',
                name: 'AddGoods',
                component: () => import('../components/goods/AddGoods.vue')
            },
            {
                path: '/sortParameter',
                name: 'SortParameter',
                component: () => import('../components/goods/SortParameter.vue')
            },
            {
                path: '/goodsSort',
                name: 'GoodsSort',
                component: () => import('../components/goods/GoodsSort.vue')
            },
            {
                path: '/orderList',
                name: 'OrderList.vue',
                component: () => import('../components/order/OrderList.vue')
            },
            {
                path: '/report',
                name: 'Report.vue',
                component: () => import('../components/data/Report.vue')
            },

        ]
    }

];

const router = new VueRouter({
    mode: 'history',
    routes
});

// 采用路由导航守卫
router.beforeEach((to, from, next) => {
    // 获取token
    const tokenStr = window.sessionStorage.getItem('token');
    if (to.name !== 'Login') {
        if (tokenStr) {
            next();
        } else {
            next('/login');
        }
    } else {
        if (tokenStr) {
            next('/');
        } else {
            next();
        }
    }
});


export default router
